export function transformToNestedSchedule(originalData: Array<{
    specificDate: string;
    timeRange?: string;
    timeSlot?: { startTime: string; endTime: string; slotId?: number };
    course?: { courseId: number; courseName: string; category?: string };
    teacher?: { teacherName: string; title?: string; specialty?: string };
    classroom?: { roomNumber: string };
    displayName?: string;
    scheduleId?: number;
}>): Record<string, Record<string, any>> {
    const result: Record<string, Record<string, any>> = {};

    originalData.forEach(item => {
        // 使用 specificDate 来计算星期几，而不是 weekNumber
        const day = getWeekDayNameFromDate(item.specificDate);

        // 获取时间段 - 根据数据结构调整
        const timeRange = item.timeRange ||
            (item.timeSlot ? `${item.timeSlot.startTime}-${item.timeSlot.endTime}` : '');

        if (!result[day]) {
            result[day] = {};
        }

        // 跳过没有课程信息的活动（如班会）
        if (!item.course || !item.teacher) {
            return;
        }

        result[day][timeRange] = {
            id: `C${item.course.courseId.toString().padStart(3, '0')}`,
            name: item.course.courseName,
            teacher: item.teacher.teacherName,
            teacherInfo: `${item.teacher.teacherName}，${item.teacher.title}${item.teacher.specialty ? '，' + item.teacher.specialty : ''}`,
            classroom: item.classroom?.roomNumber || '未知教室',
            description: item.displayName,
            category: item.course.category,
            scheduleId: item.scheduleId,
            specificDate: item.specificDate,
            slotId: item.timeSlot?.slotId,
            // 添加计算出的星期信息
            weekDay: getWeekDayNumberFromDate(item.specificDate),
            time: timeRange,
            day: day
        };
    });

    return result;
}

// 从日期字符串获取星期几的数字（1=周一, 2=周二, ..., 7=周日）
function getWeekDayNumberFromDate(dateString: string): number {
    if (!dateString) return 0;

    const date = new Date(dateString);
    const day = date.getDay(); // 0=周日, 1=周一, ..., 6=周六

    // 转换为中国的习惯：1=周一, 2=周二, ..., 7=周日
    return day === 0 ? 7 : day;
}

// 从日期字符串获取星期几的名称
function getWeekDayNameFromDate(dateString: string): string {
    if (!dateString) return "未知";

    const weekDayNumber = getWeekDayNumberFromDate(dateString);
    const weekDays = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];

    // 确保索引在有效范围内
    return weekDayNumber >= 1 && weekDayNumber <= 7 ? weekDays[weekDayNumber - 1] : "未知";
}


export function transformClassData(classList: Array<{
    classId: number;
    className: string;
    gradeLevel: string;
    headTeacher?: string | null;
    studentCount?: number | null;
    displayName?: string;
}>): Array<{
    value: number;
    label: string;
    gradeLevel: string;
    headTeacher?: string | null;
    studentCount?: number | null;
    displayName?: string;
}> {
    return classList.map(cls => ({
        value: cls.classId,
        label: cls.displayName || `${cls.gradeLevel}${cls.className}`, // 提供默认值
        gradeLevel: cls.gradeLevel,
        headTeacher: cls.headTeacher,
        studentCount: cls.studentCount,
        displayName: cls.displayName || `${cls.gradeLevel}${cls.className}`
    }));
}


export function transformTermData(termList: Array<{
    termId: number;
    termName: string;
    startDate: string;
    endDate: string;
    isCurrent: boolean;
    formattedPeriod: string;
}>): Array<{
    value: number;
    label: string;
    startDate: string;
    endDate: string;
    isCurrent: boolean;
    formattedPeriod: string;
}> {
    return termList.map(term => ({
        value: term.termId,
        label: term.termName,
        startDate: term.startDate,
        endDate: term.endDate,
        isCurrent: term.isCurrent,
        formattedPeriod: term.formattedPeriod
    }));
}

export function transformWeekData(weekList: Array<{
    weekNumber: number;
    startDate: string;
    endDate: string;
    displayName: string;
}>): Array<{
    value: number;
    label: string;
    name: string;
    startDate: string;
    endDate: string;
    displayName: string; // 保留原字段以便向后兼容
}> {
    return weekList.map(week => ({
        value: week.weekNumber,
        label: week.displayName,
        name: week.displayName,
        startDate: week.startDate,
        endDate: week.endDate,
        displayName: week.displayName // 保留原字段
    }));
}
